package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type BusNovelsApi struct {
}

var busNovelsService = service.ServiceGroupApp.AutoCodeServiceGroup.BusNovelsService


// CreateBusNovels 创建BusNovels
// @Tags BusNovels
// @Summary 创建BusNovels
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.BusNovels true "创建BusNovels"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /busNovels/createBusNovels [post]
func (busNovelsApi *BusNovelsApi) CreateBusNovels(c *gin.Context) {
	var busNovels autocode.BusNovels
	_ = c.ShouldBindJSON(&busNovels)
	if err := busNovelsService.CreateBusNovels(busNovels); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBusNovels 删除BusNovels
// @Tags BusNovels
// @Summary 删除BusNovels
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.BusNovels true "删除BusNovels"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /busNovels/deleteBusNovels [delete]
func (busNovelsApi *BusNovelsApi) DeleteBusNovels(c *gin.Context) {
	var busNovels autocode.BusNovels
	_ = c.ShouldBindJSON(&busNovels)
	if err := busNovelsService.DeleteBusNovels(busNovels); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBusNovelsByIds 批量删除BusNovels
// @Tags BusNovels
// @Summary 批量删除BusNovels
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除BusNovels"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /busNovels/deleteBusNovelsByIds [delete]
func (busNovelsApi *BusNovelsApi) DeleteBusNovelsByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := busNovelsService.DeleteBusNovelsByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBusNovels 更新BusNovels
// @Tags BusNovels
// @Summary 更新BusNovels
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body autocode.BusNovels true "更新BusNovels"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /busNovels/updateBusNovels [put]
func (busNovelsApi *BusNovelsApi) UpdateBusNovels(c *gin.Context) {
	var busNovels autocode.BusNovels
	_ = c.ShouldBindJSON(&busNovels)
	if err := busNovelsService.UpdateBusNovels(busNovels); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBusNovels 用id查询BusNovels
// @Tags BusNovels
// @Summary 用id查询BusNovels
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocode.BusNovels true "用id查询BusNovels"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /busNovels/findBusNovels [get]
func (busNovelsApi *BusNovelsApi) FindBusNovels(c *gin.Context) {
	var busNovels autocode.BusNovels
	_ = c.ShouldBindQuery(&busNovels)
	if err, rebusNovels := busNovelsService.GetBusNovels(busNovels.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebusNovels": rebusNovels}, c)
	}
}

// GetBusNovelsList 分页获取BusNovels列表
// @Tags BusNovels
// @Summary 分页获取BusNovels列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query autocodeReq.BusNovelsSearch true "分页获取BusNovels列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /busNovels/getBusNovelsList [get]
func (busNovelsApi *BusNovelsApi) GetBusNovelsList(c *gin.Context) {
	var pageInfo autocodeReq.BusNovelsSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := busNovelsService.GetBusNovelsInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}



func (busNovelsApi *BusNovelsApi)  CreateBusNovelsByObject( uuid string , name string , author string , cover string , category string ,
	shortInfo string , retent string , latelyFollower *float64 ) autocode.BusNovels{
	s := autocode.BusNovels{
		Uuid:   uuid,
        Name:   name,
		Author:  author,
		Cover:  cover,
		MinorCate:  category,
		ShortIntro:  shortInfo,
		RetentionRatio:  retent,
		LatelyFollower: latelyFollower,
    }
	return s
}



func (busNovelsApi *BusNovelsApi) DeleteAllBusNovels() {
	busNovelsListService.DeleteAllBusNovelsList()
}